%%% Joonhee Choi 2019/05/23 %%%

% Aravi's lab data reading
% [num,txt,raw] = xlsread('worm_division_table_aravi.xlsx',1);
% aravi_temp = num(1,:);
% aravi_data = num(2:end,:);

% load the data
[~,sheet_info] = xlsfinfo('CellDivisionTimes_Temp.xlsx');

%which data to look at: category: 0: old/new worm, 1: Zen-4
category=0;

% variables to be saved 
data = cell(1, length(sheet_info));
cellstr_list = cell(1, length(sheet_info));
start_frame_list = cell(1, length(sheet_info));
end_frame_list = cell(1, length(sheet_info));
division_time_list = cell(1, length(sheet_info));
meas_temp_list = cell(1, length(sheet_info));
proj_temp_list = cell(1, length(sheet_info));

% data reading
for ii = 1:length(sheet_info)
    [num,txt,raw] = xlsread('CellDivisionTimes_Temp.xlsx',ii);
    data{ii} = raw;
    
    % cell names
    cellstr_list{ii} = data{ii}(2:end,1);
    % division start [frames]
    start_frame_list{ii} = cell2mat(data{ii}(2:end,2));
    % division end [frames]
    end_frame_list{ii} = cell2mat(data{ii}(2:end,3));
    % duration [seconds]
    division_time_list{ii} = cell2mat(data{ii}(2:end,4));
    % measured temperature [degC]
    meas_temp_list{ii} = cell2mat(data{ii}(2:end,7));
    % projected temperature [degC] by "old" calibration
    proj_temp_list{ii} = 1./(log(division_time_list{ii}/60./exp(cell2mat(data{ii}(2:end,10)))) ./ (cell2mat(data{ii}(2:end,9))*1000)) - 273;
    zero_idx = find(proj_temp_list{ii} == -273);
    proj_temp_list{ii}(zero_idx) = 0;
end

%select worm category: 0:old+new or 1:Zen4
if category==0
    proj_temp_list = proj_temp_list([1:71 85:89 109:end]);
    meas_temp_list = meas_temp_list([1:71 85:89 109:end]);
    division_time_list = division_time_list([1:71 85:89 109:end]);
    
else
    proj_temp_list = proj_temp_list([72:84 90:108]);
    meas_temp_list = meas_temp_list([72:84 90:108]);
    division_time_list = division_time_list([72:84 90:108]);
end    
% important data processing
division_time_mat = [];
meas_temp_mat = [];
proj_temp_mat = [];
for ii = 1:length(proj_temp_list)
    division_time_mat = [division_time_mat reshape(division_time_list{ii}, 14, length(division_time_list{ii})/14)];  
    meas_temp_mat = [meas_temp_mat reshape(meas_temp_list{ii}, 14, length(meas_temp_list{ii})/14)];
    proj_temp_mat = [proj_temp_mat real(reshape(proj_temp_list{ii}, 14, length(proj_temp_list{ii})/14))];
end

% filter out some unidentified data
% 14 = the number of cells up to 8-cell stage
cell_division_time = cell(1,14);
cell_measT = cell(1,14);
cell_projT = cell(1,14);
for jj = 1:14
    
    cell_division_time{jj} = division_time_mat(jj,:)/60;
    cell_measT{jj} = meas_temp_mat(jj,:);
    cell_projT{jj} = proj_temp_mat(jj,:);
    
    nonzero_idx = find(cell_projT{jj} > 0);
    
    cell_division_time{jj} = cell_division_time{jj}(nonzero_idx);
    cell_measT{jj} = cell_measT{jj}(nonzero_idx);
    cell_projT{jj} = cell_projT{jj}(nonzero_idx);
    
end

disp('done!')